package com.pda.xinma.ui.agvmove;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.pda.xinma.R;
import com.pda.xinma.bean.LatheMachinBean;
import com.pda.xinma.bean.ScanBean;
import com.pda.xinma.bean.ScanResultBean;
import com.pda.xinma.bean.SelectBean;
import com.pda.xinma.bean.request.AgvMoveRequest;
import com.pda.xinma.contract.AgvMoveContract;
import com.pda.xinma.dialog.AffirmDialog;
import com.pda.xinma.dialog.SignDialog;
import com.pda.xinma.presenter.AgvMovePresenter;
import com.pda.xinma.utils.PopUtisl;
import com.pda.xinma.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.base.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = AppActivityKey.AGVMOVEACTIVITY)
public class AgvMoveActivity extends BaseMvpActivity<AgvMovePresenter> implements AgvMoveContract.View {
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_storageBin)
    TextView tvStorageBin;
    @BindView(R.id.tv_container)
    TextView tvContainer;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    @BindView(R.id.tv_move_out)
    TextView tvMoveOut;
    @BindView(R.id.tv_sign)
    TextView tvSign;

    @Override
    public AgvMovePresenter getPresenter() {
        return new AgvMovePresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_agv_move;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initLinsenterner() {
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                if (!TextUtils.isEmpty(scanBean.getCode())) {
                    presenter.scanTheQRCodeToVerify(scanBean.getCode());
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void setScanBean(ScanResultBean scanBean) {
        if (scanBean.getType().equals("KW")) {
            tvStorageBin.setText(scanBean.getCode());
        } else /*if ()*/ {
            ToastUtils.showToast(mContext, "请扫接驳工位？");
        }
    }

    private String lathCode;

    @Override
    public void setList(List<LatheMachinBean> list) {
        List<SelectBean> selectBeans = new ArrayList<>();
        for (LatheMachinBean bean : list) {
            selectBeans.add(new SelectBean(bean.getLatheName(), bean.getLatheCode()));
        }
        PopUtisl.showSelectPop(this, selectBeans, tvContainer, new PopUtisl.SelectClick() {
            @Override
            public void onClick(SelectBean bean) {
                lathCode = bean.getId();
                tvContainer.setText(bean.getName());
            }
        }, "请选择机台");
    }

    @Override
    public void onSuccess() {
        tvStorageBin.setText("");
        tvContainer.setText("");
    }

    @Override
    public void getData() {

    }


    @OnClick({R.id.iv_close, R.id.tv_container, R.id.tv_move_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finishActivity();
                break;
            case R.id.tv_container:
                presenter.getLatheList();
                break;
            case R.id.tv_move_out:
                if (TextUtils.isEmpty(tvStorageBin.getText())) {
                    showMessage("起点不能为空！");
                    return;
                }
                if (TextUtils.isEmpty(lathCode)) {
                    showMessage("终点不能为空！");
                    return;
                }
                AgvMoveRequest request = new AgvMoveRequest();
                request.setConnectionCode(tvStorageBin.getText().toString());
                request.setLatheCode(lathCode);
                AffirmDialog affirmDialog = new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                    @Override
                    public void onConfrim() {
                        presenter.confirmCarry(request);
                    }
                });
                affirmDialog.show("确定要搬运吗？");
                break;
        }
    }


    @OnClick(R.id.tv_sign)
    public void onViewClicked() {
        SignDialog signDialog=new SignDialog(mContext);
        signDialog.show();
        sbScan.setScanCallBack(null);
        signDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
                    @Override
                    public void callBack(ScanBean scanBean) {
                        if (!TextUtils.isEmpty(scanBean.getCode())) {
                            presenter.scanTheQRCodeToVerify(scanBean.getCode());
                        }
                    }
                });
            }
        });
    }
}
